var canvasCard = require('../../../wxjs/canvasCardRank.js');
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
};

var currentPage = {
  data,
  form: {}, //详情请求参数
  onLoad,
  loadInit, //跳转到登录授权成功后返回
  getDetail,
  newCanvas, //名片生成
  // onShareAppMessage: function() {
  //   const that = this;
  //   const uuid = that.data.uuid;
  //   return {
  //     title: '',
  //     path: app.xhx_route.leagueList + "?uuid=" + uuid
  //   }
  // }
};

var page = Object.assign(app.navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const uuid = options && options.uuid || '';
  const activityUuid = options && options.activityUuid || '';
  const lectureUuid = options && options.lectureUuid || '';
  const couponUuid = options && options.couponUuid || '';
  that.setData({
    activityUuid,
    lectureUuid,
    couponUuid,
  })
  that.form.activityUuid = activityUuid;
  that.form.lectureUuid = lectureUuid;
  that.form.couponUuid = couponUuid;
  that.newCanvas();

  app.xlogin.getUser(function(res) {
    if (res) that.loadInit();

    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;
  })
}

function loadInit() {
  const that = this;
  that.getDetail();
}

function getDetail() {
  const that = this;
  let temp = {};

  app.middleware.requestAjax({
    serAddr: app.serAddrs.getUserLectureRankInfo,
    data: that.form,
    cancelToast: true,
    success: function(res) {
      let xbody = res.data;
      temp.xbody = xbody;
      that.setData(temp);

      if (xbody.createTime) {
        let arr = xbody.createTime.split(" ");
        xbody.createTime = arr[0];
      }

      if (xbody.kcalRank == 1) {
        xbody.rankText = "第一名";
      } else if (xbody.kcalRank == 2) {
        xbody.rankText = "第二名";
      } else if (xbody.kcalRank == 3) {
        xbody.rankText = "第三名";
      }

      that.myCanvas.canvasData({
        title: xbody.lectureName || '',
        lable: xbody.label || '',
        address: xbody.gymName || '',
        rank: xbody.rankText || '',
      })

      return ;

      if (xbody.qrCodeUrl) {
        app.downloadFile.downloadFile({
          url: xbody.qrCodeUrl,
          success: function(path) {
            xbody.qrCodeUrl = path;

            if (xbody.headPortrait) {
              app.downloadFile.downloadFile({
                url: xbody.headPortrait,
                success: function(path) {
                  xbody.headPortrait = path;
                  temp.xbody = xbody;
                  that.setData(temp);
                  that.myCanvas.canvasData({
                    headimg: xbody.headPortrait || '',
                    nickName: xbody.username || '',
                    code: xbody.qrCodeUrl || '',
                    cardDays: xbody.createTime || '',
                  })
                }
              })
            }

          }
        })
      }
    }
  })
}

/**
 * 名片生成
 */
function newCanvas() {
  var that = this;
  // var xbody = that.data.xbody || {};

  let data = {
    // title: xbody.lectureName || '',
    // lable: xbody.label || '',
    // address: xbody.gymName || '',
    // rank: xbody.rankText || '',
    bgImg: app.xhx_images.medal,
    // headimg: xbody.headPortrait,
    // nickName: xbody.username,
    // code: xbody.qrCodeUrl,
    // cardDays: xbody.createTime,
  };

  console.log("海报卡片参数", data);

  if (that.myCanvas) {
    that.myCanvas.canvasData(data)
    return;
  }

  that.myCanvas = new canvasCard.canvas({
    canvasId: 'myCanvas',
    data,
    changeSize: function(res) {
      that.setData(res)
      console.log('changeSize', res)
    },
    success: function() {
      that.setData({
        msg: ''
      })
    }
  });
}